#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

import os
import string
import re
import xml.etree.ElementTree as etree

namespace = "http://schemas.android.com/apk/res/android"

#读取xml获得根节点
def read_xml(xml_file):
	tree = etree.parse(xml_file)
	return tree

def parse_attribute_name(attribute_name):
	ns = {
			'android:minSdkVersion' : '{%s}minSdkVersion' % namespace,
			'android:targetSdkVersion' : '{%s}targetSdkVersion' % namespace,
		}
	return ns[attribute_name]

def run(src_xml, save_file):
	if( False == os.path.exists(src_xml) ):
		raise Exception(src_xml + "文件不存在，请检查!")
	if( False == os.path.exists(save_file) ):
		raise Exception(save_file + "文件不存在，请检查!")

	#注册xml命名空间android,否则写文件的时候会出现'ns'
	etree.register_namespace('android', namespace)
	
	#读取xml得到application节点
	tree = read_xml(src_xml)
	root = tree.getroot()
	
	for elem in root.findall('uses-sdk'):
		elem.set('{%s}minSdkVersion'% namespace,'15')
		elem.set('{%s}targetSdkVersion'% namespace,'15')

	tree.write(save_file, "utf-8")

#调整资源
def channelCustomOperBegin():
	print("begin , this is baidu !")
	fileSrcName = "E:/AndroidManifest.xml"
	fileDesName = "E:/AndroidManifest.xml"
	run(fileSrcName,fileDesName)

#恢复资源
def channelCustomOperEnd():
	print("end , this is baidu !")

if __name__=="__main__":
#	if(sys.argv[1] == "begin"):
#		channelCustomOperBegin()
#	elif(sys.argv[1] == "end"):
#		channelCustomOperEnd()
#	else:
#		raise("call channel custom operation must with param begin or end!")
	channelCustomOperBegin()

